package com.weslide.weslideapp.fragment;

import java.util.ArrayList;
import java.util.List;

import com.weslide.weslideapp.R;

import android.R.string;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class GlobalNavigationFragment extends WeSlideFragment {
	public static final String TAG = "GlobalNavigationFragment";
	private static final String KEY_PREFIX = TAG + "KEY_PREFIX";
	private static final String STORE_KEY_HISTORY_LIST = "STORE_KEY_HISTORY_LIST";
	private static final String STORE_KEY_MAIN_LAYOUT_ID = "STORE_KEY_MAIN_LAYOUT_ID";
	private ArrayList<String> mHistoryList = new ArrayList<String>();
	private int mCurrentLayoutId;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mCurrentLayoutId = R.id.page_global_navigation_content;
		if (savedInstanceState != null) {
			mHistoryList = savedInstanceState
					.getStringArrayList(STORE_KEY_HISTORY_LIST);
		} else {
			if (mHistoryList == null) {
				mHistoryList = new ArrayList<String>();
			}
		}
		return inflater.inflate(R.layout.page_global_navigation, container,
				false);
	}

	public List<String> getHistory() {
		return mHistoryList;
	}

	public String push(WeSlideFragment fragment, boolean isNeedRemove) {
		String key = KEY_PREFIX + "_" + mHistoryList.size();
		FragmentTransaction transaction = getChildFragmentManager()
				.beginTransaction();
		transaction.setCustomAnimations(R.anim.slide_in_right,
				R.anim.slide_out_left);
		WeSlideFragment currentFragment = getCurrentFragment();
		if (currentFragment != null) {
			if (isNeedRemove) {
				transaction.remove(currentFragment);
				mHistoryList.remove(mHistoryList.size() - 1);
			} else {
				transaction.hide(currentFragment);
			}
		}
		transaction.add(mCurrentLayoutId, fragment, key);
		transaction.commitAllowingStateLoss();
		mHistoryList.add(key);

		return key;

	}

	public void popToFragmentByKey(String key) {
		int keyIndex = mHistoryList.indexOf(key);
		if (keyIndex >= 0) {
			if (keyIndex == mHistoryList.size() - 1) {
				return;
			}
			List<String> removeKeyList = new ArrayList<String>();
			FragmentTransaction transaction = getChildFragmentManager()
					.beginTransaction();
			transaction.setCustomAnimations(R.anim.slide_in_left,
					R.anim.slide_out_right);
			for (int i = keyIndex + 1; i < mHistoryList.size(); i++) {
				String tag = mHistoryList.get(i);
				removeKeyList.add(tag);
				Fragment fragment = getChildFragmentManager()
						.findFragmentByTag(tag);
				transaction.remove(fragment);
			}
			mHistoryList.removeAll(removeKeyList);
			WeSlideFragment fragment = (WeSlideFragment) getChildFragmentManager()
					.findFragmentByTag(key);
			transaction.show(fragment);
			transaction.commit();
		}
	}

	public void pop() {
		if (mHistoryList.size() > 1) {
			String key = mHistoryList.get(mHistoryList.size() - 2);
			popToFragmentByKey(key);
		}
	}

	public void popToRootFragment() {
		if (mHistoryList.size() > 0) {
			String key = mHistoryList.get(0);
			popToFragmentByKey(key);
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putStringArrayList(STORE_KEY_HISTORY_LIST, mHistoryList);
		super.onSaveInstanceState(outState);
	}

	public WeSlideFragment getCurrentFragment() {
		WeSlideFragment fragment = null;
		if (mHistoryList.size() > 0) {
			String currentKey = mHistoryList.get(mHistoryList.size() - 1);
			fragment = (WeSlideFragment) getChildFragmentManager()
					.findFragmentByTag(currentKey);
		}
		return fragment;
	}

	public void setRootFragment(WeSlideFragment mRootFragment) {
		FragmentTransaction transaction = getChildFragmentManager()
				.beginTransaction();
		if (mHistoryList != null) {
			for (int i = 0; i < mHistoryList.size(); i++) {
				String tag = mHistoryList.get(i);
				Fragment fragment = getChildFragmentManager()
						.findFragmentByTag(tag);
				transaction.remove(fragment);
			}
		}
		String fragmentKey = KEY_PREFIX + "_0";
		transaction.add(mCurrentLayoutId, mRootFragment, fragmentKey);
		transaction.commit();
		mHistoryList.add(fragmentKey);
	}
}
